package com.huidian.resource.server.service;

import com.huidian.cache.aop.ClearCache;
import com.huidian.resource.client.enums.EmBaseCacheDef;
import com.huidian.resource.server.dao.StudentMapper;
import com.huidian.resource.server.entity.Student;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author tjs
 */
@Service
public class StudentServiceImpl implements IStudentService {
    @Resource
    private StudentMapper studentMapper;

    @Override
    public List<Student> selectList(Student student) {
        return studentMapper.select(student);
    }

    @Override
    public Student selectById(Long stdId) {
        return studentMapper.selectByPrimaryKey(stdId);
    }

    @Override
    public long selectTotal(Student student) {
        return studentMapper.selectCount(student);
    }

    @Override
    @ClearCache(idKey = EmBaseCacheDef.STUDENT_ID_KEY,idCache = EmBaseCacheDef.STUDENT_ID_CACHE,indexKey = EmBaseCacheDef.STUDENT_INDEX_KEY,indexCache = EmBaseCacheDef.STUDENT_INDEX_CACHE,pageCache = EmBaseCacheDef.STUDENT_PAGE_CACHE,
            cacheManager = EmBaseCacheDef.STUDENT_CACHE_MANAGER,deleteKeys = EmBaseCacheDef.STUDENT_CACHE_DEL_METHOD + "#list)" )
    public void addBatch(List<Student> list) {
        for (Student student: list) {
            studentMapper.insert(student);
        }
    }


    @Override
    @ClearCache(idKey = EmBaseCacheDef.STUDENT_ID_KEY,idCache = EmBaseCacheDef.STUDENT_ID_CACHE,indexKey = EmBaseCacheDef.STUDENT_INDEX_KEY,indexCache = EmBaseCacheDef.STUDENT_INDEX_CACHE,pageCache = EmBaseCacheDef.STUDENT_PAGE_CACHE,
            cacheManager = EmBaseCacheDef.STUDENT_CACHE_MANAGER,deleteKeys = EmBaseCacheDef.STUDENT_CACHE_DEL_METHOD + "#student)" )
    public void addSelective(Student student) {
        studentMapper.insertSelective(student);
    }

    @Override
    @ClearCache(idKey = EmBaseCacheDef.STUDENT_ID_KEY,idCache = EmBaseCacheDef.STUDENT_ID_CACHE,indexKey = EmBaseCacheDef.STUDENT_INDEX_KEY,indexCache = EmBaseCacheDef.STUDENT_INDEX_CACHE,pageCache = EmBaseCacheDef.STUDENT_PAGE_CACHE,
            daoBeanName = "studentServiceImpl",daoMethodName = "selectBatchIds",daoParamClazz = {Collection.class},
            cacheManager = EmBaseCacheDef.STUDENT_CACHE_MANAGER,deleteKeys = EmBaseCacheDef.STUDENT_CACHE_DEL_METHOD + "#student)"
    )
    public void updateObj(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    @ClearCache(idKey = EmBaseCacheDef.STUDENT_ID_KEY,idCache = EmBaseCacheDef.STUDENT_ID_CACHE,indexKey = EmBaseCacheDef.STUDENT_INDEX_KEY,indexCache = EmBaseCacheDef.STUDENT_INDEX_CACHE,pageCache = EmBaseCacheDef.STUDENT_PAGE_CACHE,
            daoBeanName = "studentServiceImpl",daoMethodName = "selectBatchIds",daoParamClazz = {Collection.class},
            cacheManager = EmBaseCacheDef.STUDENT_CACHE_MANAGER,deleteKeys = EmBaseCacheDef.STUDENT_CACHE_DEL_METHOD + "#studentList)"
    )
    public void batchUpdateObj(List<Student> studentList) {
        for (Student student:studentList) {
            updateObj(student);
        }
    }


    @Override
    @ClearCache(idKey = EmBaseCacheDef.STUDENT_ID_KEY,idCache = EmBaseCacheDef.STUDENT_ID_CACHE,indexKey = EmBaseCacheDef.STUDENT_INDEX_KEY,indexCache = EmBaseCacheDef.STUDENT_INDEX_CACHE,pageCache = EmBaseCacheDef.STUDENT_PAGE_CACHE,
            daoBeanName = "studentServiceImpl",daoMethodName = "selectBatchIds",daoParamClazz = {Collection.class},
            cacheManager = EmBaseCacheDef.STUDENT_CACHE_MANAGER,deleteKeys = EmBaseCacheDef.STUDENT_CACHE_DEL_METHOD + "#stdId)"
    )
    public void deleteById(Long stdId) {
        studentMapper.deleteByPrimaryKey(stdId);
    }

    @Override
    @ClearCache(idKey = EmBaseCacheDef.STUDENT_ID_KEY,idCache = EmBaseCacheDef.STUDENT_ID_CACHE,indexKey = EmBaseCacheDef.STUDENT_INDEX_KEY,indexCache = EmBaseCacheDef.STUDENT_INDEX_CACHE,pageCache = EmBaseCacheDef.STUDENT_PAGE_CACHE,
            daoBeanName = "studentServiceImpl",daoMethodName = "selectBatchIds",daoParamClazz = {Collection.class},
            cacheManager = EmBaseCacheDef.STUDENT_CACHE_MANAGER,deleteKeys = EmBaseCacheDef.STUDENT_CACHE_DEL_METHOD + "#stdIdList)"
    )
    public void deleteByIdList(List<Long> stdIdList) {
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("stdId",stdIdList);
        studentMapper.deleteByExample(example);
    }

    @Override
    public List<Student> selectBatchIds(Collection<Long> stdIdList) {
        Example example = new Example(Student.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("stdId",stdIdList);
        return studentMapper.selectByExample(example);
    }
}
